package com.market.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtil
 * @Description: 日期工具类
 * @author wangXi
 * @date 2015年6月10日 上午10:22:27
 */
public class DateUtil {

	/**
	 * @Title: formatDate
	 * @Description: 格式化日期
	 * @param date
	 * @param formatString
	 * @return String
	 */
	public static String formatDate(Date date, String formatString) {
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		return format.format(date);
	}

	/**
	 * @Title: fromStringToDate
	 * @Description: 自定义日期，注意 日期字符串需和fomat保持一致
	 * @param fomat
	 *            例：yyyy-MM-dd
	 * @param dateStr
	 *            例：2015-01-01
	 * @return Date
	 * @throws ParseException
	 */
	public static Date fromStringToDate(String fomat, String dateStr)
			throws ParseException {
		Date date = null;
		date = new SimpleDateFormat(fomat).parse(dateStr);
		return date;
	}

	/**
	 * @Title: currentDayMinus
	 * @Description: 根据系统参数对当前日期进行相减操作
	 * @param formatString
	 *            日期格式
	 * @param days
	 * @return String 当前日期相减后的日期
	 */
	@SuppressWarnings("static-access")
	public static String currentDayMinus(int days, String formatString) {
		DateFormat format = new SimpleDateFormat(formatString);
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(c.DATE, -days);
		Date tempDate = c.getTime();
		String currentDayMinus = format.format(tempDate);
		return currentDayMinus;
	}
	
	/**
	 * @Title: currentDayMinus
	 * @Description: 根据系统参数对当前日期进行相减操作
	 * @param formatString
	 *            日期格式
	 * @param days
	 * @return String 当前日期相加后的日期
	 */
	@SuppressWarnings("static-access")
	public static String currentDayAddition(int days, String formatString) {
		DateFormat format = new SimpleDateFormat(formatString);
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(c.DATE, +days);
		Date tempDate = c.getTime();
		String currentDayMinus = format.format(tempDate);
		return currentDayMinus;
	}

	/**
	 * @Title: currentSecondPlus
	 * @Description: 对当前日期进行秒的加减运算
	 * @param second
	 *            与当前时间的差
	 * @param formatString
	 * @return String
	 */
	@SuppressWarnings("static-access")
	public static String currentSecondPlus(int second, String formatString) {
		DateFormat format = new SimpleDateFormat(formatString);
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(c.SECOND, second);
		Date tempDate = c.getTime();
		String restul = format.format(tempDate);
		return restul;
	}

	/**
	 * @Title: getCurrentDate
	 * @Description: 获取当日日期，不带时间
	 * @return
	 * @throws ParseException
	 */
	public static Date getCurrentDate() throws ParseException {
		Date today = new Date();
		String todayStr = DateUtil.formatDate(today, "yyyy.MM.dd");
		Date currentDate = DateUtil.fromStringToDate("yyyy.MM.dd", todayStr);
		return currentDate;
	}

	/**
	 * @Title: removeTimeInDate
	 * @Description: 删除日期中的时间
	 * @param date
	 * @return Date
	 * @throws ParseException
	 */
	public static Date removeTimeInDate(Date date) throws ParseException {
		String dateStr = DateUtil.formatDate(date, "yyyy.MM.dd");
		Date newDate = DateUtil.fromStringToDate("yyyy.MM.dd", dateStr);
		return newDate;
	}

	/**
	 * @Title: dateMinus
	 * @Description: 当前日期减某一日期
	 * @param endDate
	 * @param currentDate
	 * @return double
	 */
	public static double dateMinus(Date endDate, Date currentDate) {
		double longTime = currentDate.getTime() - endDate.getTime();
		double day = longTime / 1000 / 60 / 60 / 24;
		return day;
	}
	
	/**
	 * @Title: getCurrentDateTime
	 * @Description: 获取当前时间
	 * @return String
	 */
	public static String getCurrentDateTime()
    {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }
	
	public static void main(String[] args) throws ParseException {
		Date a = DateUtil.fromStringToDate("yyyy/MM/dd","2015/11/02 15:22:32");
		System.out.println(a);
		
	}

}
